#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/i2c.h>
#include <linux/spi/spi.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <trace/events/spi.h>

#include "icm20608reg.h"

struct icm20608_data {
	signed int gyro_x_adc;		/* 陀螺仪X轴原始值 	 */
	signed int gyro_y_adc;		/* 陀螺仪Y轴原始值		*/
	signed int gyro_z_adc;		/* 陀螺仪Z轴原始值 		*/
	signed int accel_x_adc;		/* 加速度计X轴原始值 	*/
	signed int accel_y_adc;		/* 加速度计Y轴原始值	*/
	signed int accel_z_adc;		/* 加速度计Z轴原始值 	*/
	signed int temp_adc;		/* 温度原始值 			*/
};

static struct icm20608_data icm20608data;


static int major = 0;
static int cs_gpios;
static struct class *icm20608_class;
static struct spi_device *spi_dev;
static struct device_node	*node;	

static int icm20608_read_regs(struct spi_device *spi, u8 reg, void *buf, int len)
{

	u8 data = 0;
	data = reg | 0x80;   /* 读寄存器数据时，寄存器地址最高位要置1 */
	spi_write_then_read(spi, &data, 1, buf, len);
	return 0;

}


static int icm20608_write_regs(struct spi_device *spi, u8 reg, u8 *buf, u8 len)
{

	u8 *txdata;

	txdata = kzalloc(len + 1, GFP_KERNEL);
	
	txdata[0] = reg & (~0x80);  /* 向寄存器写数据时，寄存器地址最高位要置0 */
	memcpy(&txdata[1], buf, len);
	spi_write(spi, txdata, len + 1);

	kfree(txdata);
	return 0;
}


static void icm20608_write_onereg(struct spi_device *spi, u8 reg, u8 value)
{
	u8 buf = value;
	icm20608_write_regs(spi, reg, &buf, 1);
}

static unsigned char icm20608_read_onereg(struct spi_device *spi, u8 reg)
{
	u8 data = 0;
	icm20608_read_regs(spi, reg, &data, 1);
	return data;
}


void icm20608_reginit(void)
{
	u8 value = 0;

	icm20608_write_onereg(spi_dev, ICM20_PWR_MGMT_1, 0x80);
	mdelay(50);

	icm20608_write_onereg(spi_dev, ICM20_PWR_MGMT_1, 0x01);
	mdelay(50);

	value = icm20608_read_onereg(spi_dev, ICM20_WHO_AM_I);
	printk("ICM20608 ID = %#X\n", value);		

	icm20608_write_onereg(spi_dev, ICM20_SMPLRT_DIV, 0x00); 	/* 输出速率是内部采样率					*/
	icm20608_write_onereg(spi_dev, ICM20_GYRO_CONFIG, 0x18); 	/* 陀螺仪±2000dps量程 				*/
	icm20608_write_onereg(spi_dev, ICM20_ACCEL_CONFIG, 0x18); 	/* 加速度计±16G量程 					*/
	icm20608_write_onereg(spi_dev, ICM20_CONFIG, 0x04); 		/* 陀螺仪低通滤波BW=20Hz 				*/
	icm20608_write_onereg(spi_dev, ICM20_ACCEL_CONFIG2, 0x04); /* 加速度计低通滤波BW=21.2Hz 			*/
	icm20608_write_onereg(spi_dev, ICM20_PWR_MGMT_2, 0x00); 	/* 打开加速度计和陀螺仪所有轴 				*/
	icm20608_write_onereg(spi_dev, ICM20_LP_MODE_CFG, 0x00); 	/* 关闭低功耗 						*/
	icm20608_write_onereg(spi_dev, ICM20_FIFO_EN, 0x00);		/* 关闭FIFO						*/

}

void icm20608_readdata(struct icm20608_data *datas)
{
	unsigned char data[14];
	icm20608_read_regs(spi_dev, ICM20_ACCEL_XOUT_H, data, 14);

	datas->accel_x_adc = (signed short)((data[0] << 8) | data[1]); 
	datas->accel_y_adc = (signed short)((data[2] << 8) | data[3]); 
	datas->accel_z_adc = (signed short)((data[4] << 8) | data[5]); 
	datas->temp_adc    = (signed short)((data[6] << 8) | data[7]); 
	datas->gyro_x_adc  = (signed short)((data[8] << 8) | data[9]); 
	datas->gyro_y_adc  = (signed short)((data[10] << 8) | data[11]);
	datas->gyro_z_adc  = (signed short)((data[12] << 8) | data[13]);
}


static int icm20608_open(struct inode *inode, struct file *file)
{	
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	
	file->private_data = &icm20608data; 
	
	return 0;
}

static ssize_t icm20608_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	signed int data[7];
	long err = 0;
	struct icm20608_data *datas = (struct icm20608_data *)file->private_data;

	icm20608_readdata(datas);
	data[0] = datas->gyro_x_adc;
	data[1] = datas->gyro_y_adc;
	data[2] = datas->gyro_z_adc;
	data[3] = datas->accel_x_adc;
	data[4] = datas->accel_y_adc;
	data[5] = datas->accel_z_adc;
	data[6] = datas->temp_adc;
	err = copy_to_user(buf, data, sizeof(data));
	return 0;
}

static int icm20608_release(struct inode *inode, struct file *filp)
{
 	return 0;
}


static struct file_operations icm20608_ops = {
    .owner   = THIS_MODULE,
    .open    = icm20608_open,
    .read    = icm20608_read,
    .release = icm20608_release,
};

static int icm20608_spi_probe(struct spi_device *spi)
{
	int ret = 0;
	
    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	
    major = register_chrdev(0,"icm20608",&icm20608_ops);

    icm20608_class = class_create(THIS_MODULE,"icm20608_class");
    device_create(icm20608_class, NULL, MKDEV(major, 0), NULL, "icm20608"); 

	
	node = of_find_node_by_path("/soc/aips-bus@02000000/spba-bus@02000000/ecspi@02010000");
	if(node == NULL) {
		printk("ecspi3 node not find!\n");
		//return -EINVAL;
	} 

	cs_gpios = of_get_named_gpio(node, "cs-gpios", 0);
	if(cs_gpios < 0) {
		printk("can't get cs-gpios\n");
		//return -EINVAL;
	}

	ret = gpio_direction_output(cs_gpios, 1);
	if(ret < 0) {
		printk("can't set gpio!\n");
	}

	 /* init spi_device */
 	spi->mode = SPI_MODE_0; /*MODE0，CPOL=0，CPHA=0*/
 	spi_setup(spi);
	
	spi_dev = spi;

	/* init register */
	icm20608_reginit();
		
    return 0; 

}

static int icm20608_spi_remove(struct spi_device *spi)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	
    device_destroy(icm20608_class, MKDEV(major, 0));
    class_destroy(icm20608_class);

    unregister_chrdev(major,"icm20608");

	return 0;
}

static const struct of_device_id icm20608_of_match[] = {
	{ .compatible = "invensense,icm20608" },
	{ }
};

static const struct spi_device_id icm20608_id[] = {
	{"icm20608",	0},
	{ }
};


static struct spi_driver icm20608_spi_driver = {
	.probe		= icm20608_spi_probe,
	.remove     = icm20608_spi_remove,
	.driver = {
		.name	= "icm20608",
		.owner = THIS_MODULE,
		.of_match_table = icm20608_of_match,
	},
	.id_table	= icm20608_id,
};

#if 0
static int __init icm20608_init(void)
{
	return spi_register_driver(&icm20608_spi_driver);
}

static void __exit icm20608_exit(void)
{
	spi_unregister_driver(&icm20608_spi_driver);
}

module_init(icm20608_init);
module_exit(icm20608_exit);
#else

module_spi_driver(icm20608_spi_driver);

#endif

MODULE_DESCRIPTION("icm20608 SPI Controller driver");
MODULE_AUTHOR("huahui");
MODULE_LICENSE("GPL");
